package redis

import (
	logHelper "ShortLink/Core/LogHelper"
	"fmt"

	"github.com/go-redis/redis"
	"github.com/spf13/viper"
)

var Rdb *redis.Client
var Prefix string

func Init() {
	Prefix = viper.GetString("redis.prefix")

	addr := viper.GetString("redis.addr")
	password := viper.GetString("redis.password")
	db := viper.GetInt("redis.db")
	poolSize := viper.GetInt("redis.poolSize")
	minIdleConn := viper.GetInt("redis.minIdleConn")

	Rdb = redis.NewClient(&redis.Options{
		Addr:         addr,
		Password:     password,
		DB:           db,
		PoolSize:     poolSize,
		MinIdleConns: minIdleConn,
	})

	pong, err := Rdb.Ping().Result()
	if err != nil {
		logHelper.Logger.Error(err.Error())
	} else {
		fmt.Println(pong)
	}
}
